<--- %%NOBANNER%% --> WinChk.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Check to see if a word or excel window is opened already.          |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
|    win= or window= or just a file reference;                       |
|    fileref= file reference;                                        |
|        Note: the fileref and win do not need to be both present;   |
|    return=: if you don't want a return value,                      |
|           "F" or "False" - No return value                         |
|           "T" or "" or "True" - return a value;                    |
|------------------<-- End of Arguments Needed-->--------------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %WinChk(excel, return = F);                               |
|          %WinChk(win=wordsys, return=T);                           |
|          %WinChk(wordsys);                                         |
| Usage:   %WinChk(win=, fileref=, return=);                         |
\-------------------<-- End of Files Created-->---------------------*/
%macro WinChk/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  10-26-2001 10:25pm;               |
| Modified: 11-4-2001 9:11pm;                 |
| Purpose:  Check to see if a word or excel   |
|           window is opened;                 |
\--------------------------------------------*/
%local dsid rc;
/*Read the first argument of the input;*/
%let arg=%qscan(&syspbuff,1,%str((),));
%put %index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WIN=));
%put;
/*if it includes "win=" or "window=" parameters, it will take input after "=" as the */
/*  first file reference;*/
/*if it does not have "=", then use the entire first argument as the file reference;*/
/*if not argument is provided, make a null file reference;*/
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WIN=))) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WINDOW=)))
      %then %do;
   %let win=%qscan(&arg, 2, %str(=));
   %put --> Checking the status of window "&win" to see if it is opened.;
%end;
%else %if &arg ne %then %do;
   %let win=&arg;
   %put --> Checking the status of window "&win" to see if it is opened.;
%end;
%else %if (not %SYSFUNC(FEXIST(&win))) or 
          (%index(%upcase(&win),WORD)) or 
          (%index(%upcase(&win),EXCEL)) %then %do;
   %let win= word;
   %put --> You forgot to give me an argument or you gave me a window name that;
   %put --> I can%str(%')t recognize. (I can check Microsoft Word and Excel only.);
   %put --> I am going to take "word" as default. Please, check your input.;
%end;
/*if the input is a valid file reference, try to open it;*/
/*if it is not a valid file reference, check if it has a substring of "word" or "excel",*/
/*  if so try to open "word" or "excel" accordingly;*/
/*if the file reference is not valid nor does it have "word" or "excel" substring, use*/
/*  word as default;*/
%if (%SYSFUNC(FEXIST(&win))) %then %do;
      %let dsid=%sysfunc(fopen(&win,o,132,e));
%end;
%else %if (%index(%upcase(&win),WORD)) or (%index(%upcase(&win),EXCEL)) %then %do;
   %if (%index(%upcase(&win),WORD)) %then %do;
      %let dsid=%sysfunc(fopen(wordsys,o,132,e));
   %end;
   %else %if (%index(%upcase(&win),EXCEL)) %then %do;
      %let dsid=%sysfunc(fopen(excelsys,o,132,e));
   %end;
%end;
/*if the file reference can be opened, the file is opened at the time;*/
/*otherwise it is not;*/
%if &dsid %then %do;
   %put --> Window "&win" is opened already.; 
   %if not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(RETURN=F))) %then %do; &dsid %end;
   %let rc=%sysfunc(fclose(&dsid));
%end;
%else %do;
   %put  --> Window "&win" isn%str(%')t opened.;
   %if not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(RETURN=F))) %then %do; &dsid %end;
%end;
%put; 
%mend WinChk;